--[[
描述 ： UI通用工具
作者 ： 付大志
时间 ： 13/08/08
]]
UIUtils = {};
--[[
功能   ：把得到的系统时间设置成固定的格式
参数   ：无
返回   ： 行数数组
]]
function UIUtils.getSTime()

  -- 获取手机系统时间
  local year, month, day, hour, minute, second = MLNative.getSystemTime();

  if tonumber(month) < 10 then
    month = '0'..month;
  end
  if tonumber(day) < 10 then
    day = '0'..day;
  end
  if tonumber(hour) < 10 then
    hour = '0'..hour;
  end
  if tonumber(minute) < 10 then
    minute = '0'..minute;
  end
  if tonumber(second) < 10 then
    second = '0'..second;
  end

  local sysTime = year..'-'..month..'-'..day..' '..hour..':'..minute..':'..second;
  --  local sysTime = year..'-'..month..'-'..day;

  return sysTime ;

end
--[[
功能   ：计算文本行数
参数   ：s   --  文本
		width   --  显示宽度
		maxLineLimit --  给出限制，超出限制用...代替
		fh  --  字体宽度
		fs  --  字体样式
返回   ： 行数数组
]]
function UIUtils.calcTextLine(str, width, maxLineLimit, fh, fs)
  str = tostring(str);
  local ret = {};
  local f = '[%z\1-\127\194-\244][\128-\191]*';
  local line, lastLine, isBreak = '', false, false;
  for v in str:gfind(f) do
    local limitw = width;
    if #ret == maxLineLimit-1 then

      limitw = limitw-MLNative.measStringWidth('...', fh, fs);
      lastLine=true;
    end
    if MLNative.measStringWidth(line..v, fh, fs) > limitw or v == '\n' then
      if lastLine then
        line = line..'...';
      end
      table.insert(ret, line);
      if v == '\n' then
        line = '';
      else
        line = v;
      end
    else
      line = line..v;
    end
    if #ret >= maxLineLimit then
      isBreak = true;
      break;
    end
  end
  if not isBreak then
    table.insert(ret, line);
  end
  return ret;
end


--根据数据 创建多行多列的视图
function UIUtils.makeForm_More( father, valueData )
  local w, h = father.rootView:getWH();
 
  local data_MaxPer_W, data_MinPer_W = AW(160),AW(70);  --每格的最大宽与最小宽
  local lineColor,strColor = 0xbbbbbb, 0x444444;        --线条颜色  字体颜色
  local leftColor,rightColor,titleColor = 0xf1f1f1, 0xffffff, 0xb4b4b4; --奇数格子的颜色   偶数格子的颜色   第一行格子的颜色

  local textFontHeight = AW(14);
  local dataW,dataH = nil,nil;
  local lineSpace_X,lineSpace_Y = AW(16),AW(16);  --字段间隔  横间隔  竖间隔
  local bgW,bgH = 0,0;

  local startX, startY = AW(5), AW(0);
  --计算字符串的宽度
  for i = 1,#valueData do
    local data = valueData[i];
    if dataW == nil then
      dataW = {};
      for j = 1,#data do
        data[j] = tostring(data[j]);
        dataW[j] = MLNative.measStringWidth(data[j], textFontHeight) + AW(5);
        if dataW[j] > data_MaxPer_W then  dataW[j] = data_MaxPer_W end;
      end
    else
      for j = 1,#data do
        if dataW[j] < data_MaxPer_W then
          data[j] = tostring(data[j]);
          local tempW = MLNative.measStringWidth(data[j]..' ', textFontHeight);
          if dataW[j] < tempW then
            dataW[j] = tempW;
            if dataW[j] > data_MaxPer_W then  dataW[j] = data_MaxPer_W end;
          end
        end
      end
    end
  end

  --计算字符串的高度
  dataH = {};
  for i = 1,#valueData do
    local data = valueData[i];
    dataH[i] = 0;
    for j = 1,#data do
      local tempHeight = MLNative.measStringHeight(data[j], textFontHeight, nil ,dataW[j]);
      if dataH[i] < tempHeight then dataH[i] = tempHeight end;
    end
  end

  --计算整个背景的宽
  bgW = startX + lineSpace_X/2;
  for i = 1,#dataW -1  do
    bgW = bgW + dataW[i] + lineSpace_X;
  end
  bgW = bgW + dataW[#dataW] + lineSpace_X/2
  bgW = bgW + startX;

  --计算整个背景的高
  -- bgH = startY + lineSpace_Y/2;
  for i = 1,#dataH do
    bgH = bgH + dataH[i] + lineSpace_Y;
  end
  -- bgH = bgH + dataH[#dataH] + lineSpace_Y/2;


  local detailView = MLView.new():initView(bgW,3*h,father);

  --画第一行表格背景
  detailView:getGraphic():fillRect( startX, startY, bgW - startX*2, dataH[1] + lineSpace_Y , titleColor, nil);
  --画列项表格背景
  local tempX = startX;
  for i = 1,#dataW do
    local tempColor = leftColor;
    if i%2 == 1 then
      tempColor = rightColor;
    end
    detailView:getGraphic():fillRect( tempX, dataH[1] + lineSpace_Y, dataW[i] + lineSpace_X, bgH - dataH[1] - lineSpace_Y, tempColor, nil);
    tempX = tempX + dataW[i] + lineSpace_X;
  end


  --画第一根横线
  detailView:getGraphic():drawLine(startX,startY,bgW - startX,startY,lineColor,1);
  local baseY = startY + lineSpace_Y/2;
  for i = 1,#valueData do
    local  dataH,tempH = 0,0;
    local data = valueData[i];
    local baseX = startX + lineSpace_X/2;
    for j = 1,#data do
      tempH =  MLNative.measStringHeight(data[j], textFontHeight, nil, dataW[j]);
      if dataH < tempH then dataH = tempH end;
    end
    for j=1,#data do

      local stringW = MLNative.measStringWidth(data[j], textFontHeight)
      local XX = baseX
      --         local XX = nil;
      --         if stringW > dataW[j] then
      --            XX = baseX
      --         else
      --          XX = baseX  + ((dataW[j]-stringW)/2 );
      --          end
      detailView:getGraphic():drawRectString(data[j], XX , baseY, dataW[j] , dataH, strColor, textFontHeight);
      baseX = baseX + dataW[j] + lineSpace_X;
    end
    baseY = baseY + dataH + lineSpace_Y;
    detailView:getGraphic():drawLine(startX, baseY - lineSpace_Y/2 - 1, bgW - startX,  baseY - lineSpace_Y/2 - 1, UIColor.COLOR_GRAY, 1);
  end

  local lineE = bgH;
  local lineX = startX;
  for i = 1,#dataW do
    detailView:getGraphic():drawLine(lineX, startY, lineX, lineE, UIColor.COLOR_GRAY, 1);
    lineX = lineX + dataW[i] + lineSpace_X;
  end
  detailView:getGraphic():drawLine(lineX, startY, lineX, lineE, UIColor.COLOR_GRAY, 1);
  bgH = bgH + AW(5);

  detailView:setWH( bgW, bgH);

  return detailView;
end

--根据数据 创建两列多行的视图
function UIUtils:makeV_Detail( father, items )
  local w, h = father.rootView:getWH();
  
  local keyData = {}
  local valueData = {}
  for i =1 ,#items do
    keyData[i] =items[i][1] or ''
    valueData[i] =items[i][2] or ''
  end
  local detailView = MLView.new():initView(w, 3*h,father);

  local height = AW(10);
  detailView:getGraphic():drawLine(AW(20), height - AW(5), AW(300), height - AW(5), UIColor.COLOR_GRAY, 1);

  local keyW,dataW,keyX, dataX,textFontHeight;
  keyW = AW(100);
  keyX = AW(20);
  dataX = keyX + keyW + AW(10)
  dataW = AW(170);
  textFontHeight = AW(14);
  for i=1, #keyData do
--    print('绘画：',keyData[i],valueData[i])
    local strh = MLNative.measStringHeight(keyData[i], textFontHeight, nil, keyW);
    local strDatah = MLNative.measStringHeight(tostring(valueData[i])..' ',textFontHeight, nil, dataW);
    detailView:getGraphic():drawRectString(keyData[i], keyX, height,keyW, strh, 0x444444, textFontHeight);
    detailView:getGraphic():drawRectString(valueData[i], dataX, height, dataW, strDatah, 0x444444, textFontHeight);
    if strh > strDatah then
      height = height + strh + AW(10);
    else
      height = height + strDatah + AW(10);
    end
    detailView:getGraphic():drawLine(AW(20), height - AW(5), AW(300), height - AW(5), UIColor.COLOR_GRAY, 1);
  end
  local lineE = height;
  detailView:getGraphic():drawLine(AW(125),  AW(5), AW(125), lineE- AW(5), UIColor.COLOR_GRAY, 1);
  height = height + AW(5);

  detailView:setWH( w, height);
  return detailView;
end

--根据指定标志把字符串截取成若干部分
function UIUtils.subString( str,tag)
  local i=1;
  local j=1;
  local  tArray ={};
  if string.find(str,tag,i) == nil then
    table.insert(tArray, str );
  else
    while(string.find(str,tag,i) ~= nil) do
      j= string.find(str,tag,i);
      table.insert(tArray,#tArray+1,string.sub(str,i,j-1));
      i=j+1;
    end
  end
  return  tArray
end
